package com.kpmg.datalake.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kpmg.datalake.common.constants.Constants;
import com.kpmg.datalake.common.enums.ResultCodeEnum;
import com.kpmg.datalake.common.enums.UseOrDelEnum;
import com.kpmg.datalake.common.exception.BusinessException;
import com.kpmg.datalake.common.service.IProjectService;
import com.kpmg.datalake.common.service.IPrvgUsrService;
import com.kpmg.datalake.common.thirdparty.kpmgladp.KpmgLadpInterface;
import com.kpmg.datalake.common.utils.EncrypUtil;
import com.kpmg.datalake.common.utils.ExceptionUtil;
import com.kpmg.datalake.common.vo.ServerResponse;
import com.kpmg.datalake.db.dao.SysUsrMapper;
import com.kpmg.datalake.db.model.Project;
import com.kpmg.datalake.db.model.SysUsr;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author azhao6
 * @since 2018-09-05
 */
@Service
public class PrvgUsrServiceImpl extends ServiceImpl<SysUsrMapper, SysUsr> implements
    IPrvgUsrService {

  private static final Logger logger = LoggerFactory.getLogger(PrvgUsrServiceImpl.class);
  @Resource
  private IProjectService projectService;

  /**
   * 通过用户名获取用户信息
   *
   * @param userName 用户名
   * @return 用户信息
   */
  @Override
  public SysUsr getUserByUserName(String userName) {
    SysUsr sysUsr = new SysUsr();
    sysUsr.setUsrName(userName);
    return super.selectOne(new EntityWrapper<>(sysUsr));
  }

  /**
   * 通过AD用户名 检擦校验 并创建用户
   *
   * @param userName 用户名
   * @return 用户信息
   */
  @Override
  public SysUsr autoCheckAndCreate(String userName) {
    SysUsr sysUsr = this.getUserByUserName(userName);
    if (sysUsr == null) {
      try {
        sysUsr = KpmgLadpInterface.searchUserName(userName);
        sysUsr.setUsrAcc(EncrypUtil.encryptAes(userName, Constants.ENCRY_KEY));
        //用户密码加盐
        sysUsr.setSalt(EncrypUtil.generateSalt());
        sysUsr.setUsrPwd(EncrypUtil.encryptSha256(sysUsr.getEmpid() + sysUsr.getSalt()));
        sysUsr.setCrtDt(new Date());
        sysUsr.setExpDt(new Date(32535100800000L));
        sysUsr.setDelInd(UseOrDelEnum.USE.getIntCode());
        sysUsr.setLockSt(UseOrDelEnum.USE.getIntCode());
        super.insert(sysUsr);
      } catch (Exception e) {
        logger.error("从AD获取用户信息失败，错误信息{}", e.getMessage());
      }
    }
    return sysUsr;
  }

  @Override
  public ServerResponse<SysUsr> checkUserName(String userName) {
    SysUsr sysUsr;
    try {
      //用户名
      if (StringUtils.isBlank(userName)) {
        throw new BusinessException(ResultCodeEnum.CODE4024);
      }
      sysUsr = this.getUserByUserName(userName);
      //用户为空
      if (sysUsr == null) {
        throw new BusinessException(ResultCodeEnum.CODE4023);
        //用户锁定
      } else if (!UseOrDelEnum.USE.getIntCode().equals(sysUsr.getLockSt())) {
        throw new BusinessException(ResultCodeEnum.CODE4034);
        //用户注销
      } else if (!UseOrDelEnum.USE.getIntCode().equals(sysUsr.getDelInd())) {
        throw new BusinessException(ResultCodeEnum.CODE4036);
        //过期
      } else if (sysUsr.getExpDt() == null || sysUsr.getExpDt().before(new Date())) {
        throw new BusinessException(ResultCodeEnum.CODE4037);
      }
    } catch (Exception e) {
      return ExceptionUtil.getExceptionMsg(e);
    }
    return ServerResponse.createBySuccess(sysUsr);
  }

  /**
   * 加载用户权限项目信息
   *
   * @param user 用户
   * @return 用户
   */
  @Override
  public SysUsr loadUserSettings(SysUsr user) {
    // 载入用户项目信息
    List<Project> projects = projectService.loadUserProjects(user.getUsrId());
    if (projects != null) {
      user.setProjects(projects);
    }
    return user;
  }

}
